package com.vma.interfaceShoes.controller;

import com.google.gson.Gson;
import com.jfinal.core.Controller;
import com.jfinal.kit.HttpKit;
import com.vma.interfaceShoes.util.ReturnsUtil;
import com.vma.offer.service.CartService;
import com.vma.offer.vo.CartItemVo;
import net.sf.json.JSONObject;

/**
 * Created by chenzui on 2017/4/11.
 */
public class CartController extends Controller {
    public void addItem(){
        try{
            String reqData = getPara("itemStr");
            CartItemVo cartItemVo = new Gson().fromJson(reqData,CartItemVo.class);
            CartService.addItem(cartItemVo);
            renderJson(ReturnsUtil.returnSuccess("加入购物车成功"));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }

    public void updateItem(){
        try{
            String reqData = getPara("itemStr");
            CartItemVo cartItemVo = new Gson().fromJson(reqData,CartItemVo.class);
            CartService.update(cartItemVo);
            renderJson(ReturnsUtil.returnSuccess("加入购物车成功"));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }

    public void qryCartItems(){
        try {
            Integer userId = getParaToInt("userId");
            renderJson(ReturnsUtil.returnSuccess(CartService.qryCartItems(userId)));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }

    public void delItems(){
        try {
            Integer userId = getParaToInt("userId");
            String ids = getPara("ids");
            CartService.delItems(userId,ids);
            renderJson(ReturnsUtil.returnSuccess("删除成功"));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }

    public void qryOfferInCart(){
        try {
            Integer userId = getParaToInt("userId");
            Integer offerId = getParaToInt("offerId");
            renderJson(ReturnsUtil.returnSuccess(CartService.initItemData(offerId,userId)));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }
}
